home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / str.exe / STRSEARC.CPP < prev   
Text File  |  1993-03-01  |  4KB  |  167 lines

  1. //
  2. // strsearch.cpp: str class member functions for searching and replacing
  3. // Author       : Roy S. Woll
  4. //
  5. // Copyright (c) 1993 by Roy S. Woll
  6. // You may distribute this source freely as long as you leave all files
  7. // in their original form, including the copyright notice as is.
  8. //
  9. //
  10. // Version 2.00     11/31/92
  11. //    Support searching/replacing, regular expressions, case sensitivity
  12. //
  13.  
  14. #include <string.h>
  15. #include <limits.h>
  16.  
  17. #include "str.h"
  18. #include "regx.h"
  19.  
  20.  
  21. // 
  22. // index member functions
  23. //
  24. int str::index(const char * s, int start) const{
  25.    if (caseSensitive()){
  26.       str mylowstr(lowercase(*this));
  27.       const char * charptr = strstr(mylowstr(start), lowercase(s));
  28.       if (charptr) return (charptr - mylowstr());
  29.       else return (-1);
  30.    }
  31.    else {
  32.       const char * charptr = strstr((*this)(start), s);
  33.       if (charptr) return (charptr - data->buf);
  34.       else return (-1);
  35.    }
  36. };
  37.  
  38. int str::index(const regX& reg, int start) const{
  39.    int matchlen;
  40.    return reg.index(*this, &matchlen, start, caseSensitive());
  41. };
  42.  
  43.  
  44. int str::index(const regX& reg, int * matchLen, int start) const
  45. {
  46.    return reg.index(*this, matchLen, start, caseSensitive());
  47. };
  48.  
  49.  
  50. //
  51. // search operator functions
  52. //
  53.  
  54. int str::search(const char * s, int* startPtr) const{
  55.    *startPtr = index(s, *startPtr);
  56.    return *startPtr>=0;
  57. };
  58.  
  59. int str::search(const char * s, int start) const{
  60.    return index(s, start)>=0;
  61. };
  62.  
  63. int str::search(const regX& reg, int * startPtr) const{
  64.    return search(reg, 0, startPtr);
  65. };
  66.  
  67. int str::search(const regX& reg, int start) const{
  68.    return search(reg, 0, &start);
  69. };
  70.  
  71. int str::search(const regX& reg, str * matchPtr, int start) const{
  72.    return search(reg, matchPtr, &start);
  73. };
  74.  
  75. int str::search(const regX& reg, str * matchPtr, int * startPtr) const{
  76.  
  77.    int matchLen;
  78.    int start = reg.index(*this, &matchLen, *startPtr, caseSensitive());
  79.    if (start>=0) {
  80.       if (matchPtr) *matchPtr = operator()(start, matchLen);
  81.       if (startPtr) *startPtr = start;
  82.       return 1;
  83.    }
  84.    else {
  85.       if (matchPtr) *matchPtr = "";
  86.       if (startPtr) *startPtr = -1;
  87.       return 0;
  88.    };
  89.  
  90. };
  91.  
  92.  
  93. int str::replace(const regX& reg, const char* replaceStr,
  94.                  int start, int numReplacements)
  95. {
  96.   return replace(reg, replaceStr, &start, numReplacements);
  97. };
  98.  
  99. int str::replace(const regX& reg, const char* replaceStr,
  100.                  int* startPtr, int numReplacements)
  101. {
  102.    int& start = *startPtr;
  103.    if (numReplacements==0) return 0;
  104.  
  105.    int countReplacements=0, matchLen;
  106.    int replaceLen = strlen(replaceStr);
  107.  
  108.    do {
  109.       start = index(reg, &matchLen, start);
  110.       if (start<0) break;
  111.       countReplacements++;
  112.       operator()(start, matchLen) = replaceStr;
  113.       start+= replaceLen;           //skip past newly added data
  114.  
  115.       if (!--numReplacements) break;
  116.    } while (1);
  117.  
  118.    return countReplacements;
  119.  
  120. };
  121.  
  122.  
  123.  
  124. int str::replace(const char * searchStr, const char* replaceStr,
  125.                  int start, int numReplacements)
  126. {
  127.   return replace(searchStr, replaceStr, &start, numReplacements);
  128. };
  129.  
  130. int str::replace(const char * searchStr, const char* replaceStr,
  131.                  int* startPtr, int numReplacements)
  132. {
  133.  
  134.    if (numReplacements==0) return 0;
  135.  
  136.    int& start = *startPtr;
  137.    int countReplacements=0;
  138.    int searchLen = strlen(searchStr), replaceLen = strlen(replaceStr);
  139.  
  140.    do {
  141.  
  142.       start = index(searchStr, start);
  143.       if (start<0) break;
  144.       countReplacements++;
  145.       operator()(start, searchLen) = replaceStr;
  146.       start+= replaceLen;           //skip past newly added data
  147.  
  148.       if (!--numReplacements) break;
  149.  
  150.    } while (1);
  151.  
  152.    return countReplacements;
  153.  
  154. };
  155.  
  156.  
  157. int str::replaceAll(const regX& reg, const char* replaceStr, int start)
  158. {
  159.   return replace(reg, replaceStr, &start, INT_MAX);
  160. };
  161.  
  162. int str::replaceAll(const char * searchStr, const char* replaceStr, int start)
  163. {
  164.   return replace(searchStr, replaceStr, &start, INT_MAX);
  165. };
  166.  
  167.